package com.huatai.exchange.utils;

import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.DateFormatUtils;
import com.faneui.common.utils.ListUtils;
import com.faneui.dal.DBUtils;
import com.faneui.service.Dbconfig;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.util.Date;

@Component
public class FlowUtils {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    public String isFinishO32Flow(String flowCode) throws BusinessException {
        Connection conn = null;
        try {
            O32BusinessFlow flow = O32BusinessFlow.valueOf(flowCode);
            String user = "trade_zc";
            String url = Dbconfig.TRADEDB;
            String password = Dbconfig.getPasswordByUserId(user);
            conn = DBUtils.getConnection(url, user, password);
            String sql = "select 1 from tflow where vc_flow_code = ? and c_deal_flag = '1'";
            return ListUtils.isNotEmpty(DBUtils.executeQuery(conn, sql, flow.getCode())) ? "1" : "流程节点[" + flow.getName() + "]未完成";
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        } finally {
            DBUtils.closeAll(conn);
        }
    }

    public String isFinishO32Flow(String flowCode, String occurDate) throws BusinessException {
        String date = DateFormatUtils.yyyyMMdd.format(new Date());
        if (StringUtils.isNotBlank(occurDate) && !date.equals(occurDate))
            return "1";
        return isFinishO32Flow(flowCode);
    }

    public String isFinishTaFlow(String flowCode) throws BusinessException {
        Connection conn = null;
        try {
            String user = "tathree";
            String url = Dbconfig.TRADEDB;
            String password = Dbconfig.getPasswordByUserId(user);
            conn = DBUtils.getConnection(url, user, password);
            String sql = "select 1 from ttaflowlog where c_flowcode = ? and c_dealflag = '1'";
            return ListUtils.isNotEmpty(DBUtils.executeQuery(conn, sql, flowCode)) ? "1" : "流程节点未完成";
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        } finally {
            DBUtils.closeAll(conn);
        }
    }

    public String isFinishDs1Flow(String flowCode) throws BusinessException {
        Connection conn = null;
        try {
            String user = "ds_zc1";
            String url = Dbconfig.TRADEDB;
            String password = Dbconfig.getPasswordByUserId(user);
            conn = DBUtils.getConnection(url, user, password);
            String sql = "select 1 from tdealprocess where c_flowcode = ? and c_dealflag = '1'";
            return ListUtils.isNotEmpty(DBUtils.executeQuery(conn, sql, flowCode)) ? "1" : "流程节点未完成";
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        } finally {
            DBUtils.closeAll(conn);
        }
    }

    public String isFinishDs2Flow(String flowCode) throws BusinessException {
        Connection conn = null;
        try {
            String user = "ds_zc2";
            String url = Dbconfig.TRADEDB;
            String password = Dbconfig.getPasswordByUserId(user);
            conn = DBUtils.getConnection(url, user, password);
            String sql = "select 1 from ttaflowlog where c_flowcode = ? and c_dealflag = '1'";
            return ListUtils.isNotEmpty(DBUtils.executeQuery(conn, sql, flowCode)) ? "1" : "流程节点未完成";
        } catch (Exception ex) {
            logger.error("", ex);
            throw new BusinessException(ex.getMessage());
        } finally {
            DBUtils.closeAll(conn);
        }
    }

}
